/******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
* File Name          : main.c
* Author             : Joint
* Version            : V0.2.1
* Date               : 03/22/2016
* Description        : main functions
*******************************************************************************/

#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "global.h"
#include "screen_io.h"
#include "memory.h"
#include "file.h"
#include "gcode.h"
#include "gcode_control.h"
//#include "parse.h"
#include "delay.h"
#include "display.h"
#include "tim.h"
#include "led.h"
#include "ili9320.h"
#include "interrupt.h"
#include "param.h"
#include "touch.h"
#include "manualmode.h"
#include "extruder_t.h"
#include "limitSwitch.h"
#include "keyboard.h"
#include "rs232_interface.h"
#include "stepmotor_test.h"

/***************************************************
 *  Main
 ****************************************************/
int main(void)
{
    uint8_t test = 0;
    SystemStartup();
    FileInit();
    initSmParam();      // 参数初始化
    TIM6_Init(1000);
    DBG_INFO("Init OK");
    while (1)
    {
        //        cnc_gfile("testa.gcode", GFILE_MODE_MASK_EXEC);

        test = ShowMenu("Main Menu", MENU_COUNT, MainMenu);

        switch (test)
        {
        case START_GCODE:
        {
            uint32_t stime;
            stime = Seconds();
            readFileList();
            drawFileList();
            if (currentFile == INVALID_VALUE_INT)
            {
                break;
            }
            DBG_INFO("File to print:%s", fileList[currentFile].f_name);
            cnc_gfile(fileList[currentFile].f_name, GFILE_MODE_MASK_EXEC);
            while (stepm_inProc())
            {
                scr_fontColor(Yellow, Blue);
                scr_gotoxy(1, 13);
                DBG_INFO(" remain moves: %d", stepm_getRemainLines());
                scr_clrEndl();
                stime = Seconds() - stime;
                scr_fontColor(Yellow, Blue);
                scr_gotoxy(0, 14);
                DBG_INFO("   work time: %02d:%02d", stime / 60, stime % 60);
                scr_clrEndl();
            }
            stepm_EmergeStop();
            scr_fontColor(Yellow, Blue);
            scr_gotoxy(0, 13);
            scr_puts("   FINISH. PRESS C-KEY");
            scr_clrEndl();
            WAIT_KEY_C();//wait for key-c
            break;
        }
        case MANUAL_MODE:                   // Manual Mode
            manualMode();
            break;

        case SHOW_GCODE:                  // Show GCode
            readFileList();
            drawFileList();
            cnc_gfile(fileList[currentFile].f_name, GFILE_MODE_MASK_SHOW | GFILE_MODE_MASK_CHK);
            scr_printf("\n              PRESS C-KEY");
            while (kbd_GetKey() != KEY_C_DOWN);
            break;

        case FILE_SYSTEM:                // File System
            readFileList();
            drawFileList();
            ShowFileInfo();
            if (questionYesNo("Delete file:\n'%s'?", fileList[currentFile].f_name))
            {
                f_unlink(fileList[currentFile].f_name);
            }
            break;

        case SET_TIME:                // Set time
            SetTime();
            break;
        case SCAN_MODE:                //Show File info
            win_showMsgWin();
            saveSmParam();
            scr_printf("\n\n\n        PRESS C-KEY");
            WAIT_KEY_C();
            break;
        case PARAM_SET:               //Param Set
            paramSet();
            break;                     //Touch Set
        case TOUCH_SET:
            while (Touchl_Calibrate() != 0);     /*触屏校正*/
            break;
        case STEPMOTOR_TEST:
            TB6600_TIMx_PWM_Init();
            StepMotor_Test();
            break;

        default:
            break;
        }
    }
}


